package StudentAchievementManagementSystem.ui;

import StudentAchievementManagementSystem.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static StudentAchievementManagementSystem.mysql.MysqlUtil.getConnection;

public class LoginScreen {
    public  void init()  {
        JFrame jFrame = new JFrame("学生管理系统");

        final int WIDTH=500;
        final int HEIGHT=300;
        jFrame.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);
        jFrame.setResizable(false);
        Panel panel = new Panel();
        panel.setBounds(0,0,WIDTH,HEIGHT);

        //组装登陆相关的元素
        Box vBox = Box.createVerticalBox();

        //组装账号
        Box uBox = Box.createHorizontalBox();
        JLabel label1 = new JLabel("账  号:");
        //JTextArea 是文本域，指定行和列，JTextField是文本框可以设置长度
        JTextField user = new JTextField("",15);

        uBox.add(label1);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(user);

        //组装密码
        Box pBox = Box.createHorizontalBox();

        JLabel label2 = new JLabel("密  码:");
        JTextField password = new JTextField("",15);

        pBox.add(label2);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(password);
        //登陆按钮
        Box dBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("登陆");
        JTextField out = new JTextField("登陆状态",20);
        dBox.add(loginBtn);
        dBox.add(out);

        vBox.add(Box.createHorizontalStrut(50));
        vBox.add(uBox);
        vBox.add(Box.createHorizontalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createHorizontalStrut(20));
        vBox.add(dBox);
        //添加组件
//        jFrame.add(label1);
//        jFrame.add(user);
//        jFrame.add(label2);
//        jFrame.add(password);
//        jFrame.add(out);
//        jFrame.add(jButton);
        panel.add(vBox);
        jFrame.add(panel);
        jFrame.setResizable(false);
        jFrame.setVisible(true);



        loginBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userText = user.getText();
                String pwdText = password.getText();
                //没改异常之前的代码
//                Connection connection = MysqlUtil.getConnection();
//                Statement statement = connection.createStatement();
//                String sql = "select sno,sname  from s";
//                ResultSet resultSet = statement.executeQuery(sql);
//
//                while(resultSet.next()){
//                    if (resultSet.getString(1).equals("20222101")&&resultSet.getString(2).equals("123456")){
//                        out.setText("登陆成功");
//                    }else{
//                        out.setText("登陆失败");
//                    }
//                }
                Connection connection = null;
                try {
                    connection = getConnection();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                }
                Statement statement = null;
                try {
                    statement = connection.createStatement();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }


                boolean isReg=false;//判断管理员用户是否登陆成功
                //先用resultSet登陆管理员账户
                String sqlRegAdm = "select  ano,password  from adimistor";
                ResultSet resultSetAdm = null;
                try {
                    resultSetAdm = statement.executeQuery(sqlRegAdm);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                while(true){
                    try {
                        if (!resultSetAdm.next()) break;
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                    try {
                        if (resultSetAdm.getString(1).equals(userText)&&resultSetAdm.getString(2).equals(pwdText)){
                            out.setText("管理员用户登陆成功");
                            isReg=true;
                            new ManagerInterface().init();
                            jFrame.dispose();
                            break;
                        }
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    out.setText("登陆失败");
                }


                //再用resultSet登陆学生账户
                if(isReg!=true){
                    String sqlRegStu = "select  sno,password  from student";
                    ResultSet resultSetStu = null;
                    try {
                        resultSetStu = statement.executeQuery(sqlRegStu);
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }

                    while(true){
                        try {
                            if (!resultSetStu.next()) break;
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                        try {
                            if (resultSetStu.getString(1).equals(userText)&&resultSetStu.getString(2).equals(pwdText)){
                                out.setText("学生用户登陆成功");
//                                Dialog dialog = new Dialog(jFrame,"学生用户登陆成功!请选择您的功能:");
//                                dialog.setVisible(true);
//                                dialog.setBounds(400,300,200,100);
//                                   SduMenu.sduMenu(jFrame);
//                                StuMenu.stuMenu(jFrame,userText);
                                break;
                            }
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        } out.setText("登陆失败");
                    }
                }

            }
        });
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);



    }
}
